package com.k52group.vn.micromarket.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.k52group.vn.micromarket.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.k52group.vn.micromarket.classes.Message;
import com.k52group.vn.micromarket.classes.Profile;
import com.k52group.vn.micromarket.utils.Constant;
import com.k52group.vn.micromarket.utils.HttpRequest;

public class MessageAdapter extends BaseAdapter {
	private List<Message> list;
	private LayoutInflater mInflater;
	private Context context;
	private boolean isGuest;
	Profile profile;

	public MessageAdapter(Context context, List<Message> list, Profile profile) {
		this.list = list;
		this.mInflater = LayoutInflater.from(context);
		this.context = context;
		this.profile = profile;

		if (list == null)
			list = new ArrayList<Message>();
	}

	public boolean isGuest() {
		return isGuest;
	}

	public void setGuest(boolean isGuest) {
		this.isGuest = isGuest;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.row_message, null);
			holder = new ViewHolder();
			holder.tvsender = (TextView) convertView.findViewById(R.id.TextView01);
			holder.tvmessage = (TextView) convertView.findViewById(R.id.TextView01);
			holder.tvreply = (Button) convertView.findViewById(R.id.TextView03);
			holder.tvdelete = (Button) convertView.findViewById(R.id.textView2);
			holder.tvblock = (Button) convertView.findViewById(R.id.TextView02);
			holder.tvmessage = (TextView) convertView.findViewById(R.id.textView1);
			holder.tvfavourite = (Button) convertView.findViewById(R.id.TextView04);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final Message item = list.get(position);

		holder.tvsender.setText("From: " + item.getYour_company() + "  " + item.getPhone());
		holder.tvmessage.setText("Message: " + item.getMsg() + "\nTime: " + item.getDate());

		// if (isGuest) {
		// holder.tvreply.setVisibility(View.INVISIBLE);
		// holder.tvdelete.setVisibility(View.INVISIBLE);
		// holder.tvblock.setVisibility(View.INVISIBLE);
		// } else {
		// holder.tvreply.setVisibility(View.VISIBLE);
		// holder.tvdelete.setVisibility(View.VISIBLE);
		// holder.tvblock.setVisibility(View.VISIBLE);
		// }

		holder.tvreply.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				confirmSendMessage(item.getUserId_sender() + "", item.getGcm_regid());
			}
		});
		holder.tvdelete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				confirmDelete(item.getId());
			}
		});
		holder.tvblock.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				confirmBlock(item.getUserId_sender() + "");
			}
		});
		holder.tvfavourite.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				confirmFav(item.getUserId_sender() + "");
			}
		});
		return convertView;
	}

	private static class ViewHolder {
		private TextView tvsender, tvmessage;
		private Button tvreply, tvdelete, tvblock, tvfavourite;
	}

	private void confirmSendMessage(final String reciver, final String gcm) {
		AlertDialog.Builder dialogDelete = new AlertDialog.Builder(context);
		dialogDelete.setTitle(Constant.NAME_APP);
		final EditText et = new EditText(context);
		et.setHint("Message");
		et.setSingleLine(false);
		et.setGravity(Gravity.TOP);
		et.setHeight(160);
		dialogDelete.setView(et);

		dialogDelete.setPositiveButton("Reply", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				new ReplyMessageTask(reciver, et.getText().toString(), gcm).execute();
			}
		});
		dialogDelete.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
			}
		});
		dialogDelete.setIcon(R.drawable.ic_launcher);
		dialogDelete.show();
	}

	private void confirmDelete(final int id) {
		AlertDialog.Builder dialogDelete = new AlertDialog.Builder(context);
		dialogDelete.setTitle(Constant.NAME_APP);

		dialogDelete.setMessage("Are you sure delete ?");
		dialogDelete.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				new DeleteTask(id).execute();
			}
		});
		dialogDelete.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
			}
		});
		dialogDelete.setIcon(R.drawable.ic_launcher);
		dialogDelete.show();
	}

	private void confirmBlock(final String reciver) {
		AlertDialog.Builder dialogDelete = new AlertDialog.Builder(context);
		dialogDelete.setTitle(Constant.NAME_APP);

		dialogDelete.setMessage("Are you sure block ?");
		dialogDelete.setPositiveButton("Block", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				new BlockTask(reciver).execute();
			}
		});
		dialogDelete.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
			}
		});
		dialogDelete.setIcon(R.drawable.ic_launcher);
		dialogDelete.show();
	}

	private void confirmFav(final String reciver) {
		AlertDialog.Builder dialogDelete = new AlertDialog.Builder(context);
		dialogDelete.setTitle(Constant.NAME_APP);

		dialogDelete.setMessage("Are you sure  ?");
		dialogDelete.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				new FavTask(reciver).execute();
			}
		});
		dialogDelete.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
			}
		});
		dialogDelete.setIcon(R.drawable.ic_launcher);
		dialogDelete.show();
	}

	private class ReplyMessageTask extends AsyncTask<String, String, String> {
		ProgressDialog dialog;
		String reciever;
		String message;
		String gcm;

		public ReplyMessageTask(String reciever, String message, String gcm) {
			// TODO Auto-generated constructor stub
			this.message = message;
			this.reciever = reciever;
			this.gcm = gcm;
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog = new ProgressDialog(context);
			dialog.setMessage("Sending ...");
			dialog.show();
		}

		@Override
		protected String doInBackground(String... arg0) {
			// TODO Auto-generated method stub
			HttpRequest request = HttpRequest.post(Constant.URL_SEND_MESSAGE);

			request.form("sender", profile.getUserId());
			request.form("reciever", reciever);
			request.form("msg", message);
			request.form("regId", gcm);
			System.out.println("body" + request.body());
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			dialog.dismiss();
			Toast.makeText(context, "Sent Message", Toast.LENGTH_SHORT).show();
		}
	}

	private class DeleteTask extends AsyncTask<String, String, String> {
		ProgressDialog dialog;
		int id;
		String respone;

		public DeleteTask(int id) {
			// TODO Auto-generated constructor stub
			this.id = id;
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog = new ProgressDialog(context);
			dialog.setMessage("Delete ...");
			dialog.show();
		}

		@Override
		protected String doInBackground(String... arg0) {
			// TODO Auto-generated method stub
			HttpRequest request = HttpRequest.post(Constant.URL_DELETE_MESSAGE);

			request.form("id", id);
			request.form("reciever", profile.getUserId());
			respone = request.body();
			System.out.println("body" + respone);
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			dialog.dismiss();
			Toast.makeText(context, "Deleted Message", Toast.LENGTH_SHORT).show();
			list = new Gson().fromJson(respone, new TypeToken<ArrayList<Message>>() {
			}.getType());
			if (list == null)
				list = new ArrayList<Message>();
			MessageAdapter.this.notifyDataSetChanged();
		}
	}

	private class BlockTask extends AsyncTask<String, String, String> {
		ProgressDialog dialog;
		String reciever, respone;

		public BlockTask(String reciever) {
			// TODO Auto-generated constructor stub
			this.reciever = reciever;
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog = new ProgressDialog(context);
			dialog.setMessage("Loading ...");
			dialog.show();
		}

		@Override
		protected String doInBackground(String... arg0) {
			// TODO Auto-generated method stub
			HttpRequest request = HttpRequest.post(Constant.URL_BLOCK_MESSAGE);

			request.form("UserId", profile.getUserId());
			request.form("UserId_blocked", reciever);
			respone = request.body();
			System.out.println("body" + respone);
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			dialog.dismiss();
			list = new Gson().fromJson(respone, new TypeToken<ArrayList<Message>>() {
			}.getType());
			if (list == null)
				list = new ArrayList<Message>();
			MessageAdapter.this.notifyDataSetChanged();
			Toast.makeText(context, "Blocked !", Toast.LENGTH_SHORT).show();
		}
	}

	private class FavTask extends AsyncTask<String, String, String> {
		ProgressDialog dialog;
		String reciever, respone;

		public FavTask(String reciever) {
			// TODO Auto-generated constructor stub
			this.reciever = reciever;
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog = new ProgressDialog(context);
			dialog.setMessage("Loading ...");
			dialog.show();
		}

		@Override
		protected String doInBackground(String... arg0) {
			// TODO Auto-generated method stub
			HttpRequest request = HttpRequest.post(Constant.URL_FAV_MESSAGE);

			request.form("UserId_to", profile.getUserId());
			request.form("UserId_from", reciever);
			respone = request.body();
			System.out.println("body" + respone);
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			dialog.dismiss();
			list = new Gson().fromJson(respone, new TypeToken<ArrayList<Message>>() {
			}.getType());
			if (list == null)
				list = new ArrayList<Message>();
			MessageAdapter.this.notifyDataSetChanged();
			Toast.makeText(context, "Added to favourite", Toast.LENGTH_SHORT).show();
		}
	}
}
